package com.kld.data.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kld.common.core.domain.PageQuery;
import com.kld.common.core.lang.UUID;
import com.kld.common.core.page.TableDataInfo;
import com.kld.common.utils.DateUtils;
import com.kld.data.domain.po.SysTrans;
import com.kld.data.domain.vo.SysTransVo;
import com.kld.data.mapper.SysTransMapper;
import com.kld.data.scheduler.KettleService;
import com.kld.data.scheduler.TransDataScheduler;
import com.kld.data.service.ISysTransService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Service;

import java.util.concurrent.ScheduledFuture;

/**
 * 数据转换任务Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-03
 */
@RequiredArgsConstructor
@Service
public class SysTransServiceImpl implements ISysTransService {

    private final SysTransMapper sysTransMapper;

    private final TransDataScheduler transScheduler;

    private final ThreadPoolTaskScheduler taskScheduler;

    private final KettleService kettleService;

    /**
     * 查询数据转换任务
     *
     * @param id 数据转换任务ID
     * @return 数据转换任务
     */
    @Override
    public SysTrans selectSysTransById(String id) {
        return sysTransMapper.selectSysTransById(id);
    }

    /**
     * 查询数据转换任务列表
     *
     * @param sysTrans 数据转换任务
     * @return 数据转换任务
     */
    @Override
    public TableDataInfo<SysTransVo> selectSysTransList(SysTrans sysTrans, PageQuery pageQuery) {
        Page<SysTransVo> result = sysTransMapper.selectSysTransList(sysTrans, pageQuery.build());
        return TableDataInfo.build(result);
    }

    /**
     * 新增数据转换任务
     *
     * @param sysTrans 数据转换任务
     * @return 结果
     */
    @Override
    public int insertSysTrans(SysTrans sysTrans) {
        sysTrans.setCreateTime(DateUtils.getNowDate());
        sysTrans.setId(UUID.randomUUID().toString());
        sysTrans.setTaskStatus("0");
        sysTrans.setIsView("1");
        return sysTransMapper.insertSysTrans(sysTrans);
    }

    /**
     * 修改数据转换任务
     *
     * @param sysTrans 数据转换任务
     * @return 结果
     */
    @Override
    public int updateSysTrans(SysTrans sysTrans) {
        sysTrans.setUpdateTime(DateUtils.getNowDate());
        return sysTransMapper.updateSysTrans(sysTrans);
    }

    /**
     * 批量删除数据转换任务
     *
     * @param ids 需要删除的数据转换任务ID
     * @return 结果
     */
    @Override
    public int deleteSysTransByIds(String[] ids) {
        return sysTransMapper.deleteSysTransByIds(ids);
    }

    /**
     * 删除数据转换任务信息
     *
     * @param id 数据转换任务ID
     * @return 结果
     */
    @Override
    public int deleteSysTransById(String id) {
        return sysTransMapper.deleteSysTransById(id);
    }

    @Override
    public int changeTaskStatus(SysTrans sysTrans) {
        String status = sysTrans.getTaskStatus();
        if ("1".equals(status)) {
            transScheduler.registTransTask(taskScheduler, sysTrans);
        } else {
            String id = sysTrans.getId();
            ScheduledFuture<?> task = TransDataScheduler.futureMap.get(id);
            if (task != null && !task.isCancelled()) {
                task.cancel(false);
                TransDataScheduler.futureMap.remove(id);
            }
        }
        return updateSysTrans(sysTrans);
    }

    @Override
    public int executeTransTask(SysTrans sysTrans) {

        kettleService.excuteTrans(sysTrans);
        sysTrans.setLastExecResult(2);
        return updateSysTrans(sysTrans);
    }

}
